package com.orhon.smartcampus.modules.teacher.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.teacher.dao.QualificationsOptionsDao;
import com.orhon.smartcampus.modules.teacher.entity.QualificationsOptions;
import com.orhon.smartcampus.modules.teacher.gqlInputModel.GQLQualificationsOptionInput;
import com.orhon.smartcampus.modules.teacher.inputMapper.QualificationsOptionInputMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddQualificationsOptionMutation implements DataFetcher {

    @Autowired
    QualificationsOptionsDao qualificationsOptionsDao;

    /**
     * 添加资格选项信息
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {
        GQLQualificationsOptionInput input = QualificationsOptionInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        QualificationsOptions qualificationsOptions = null;

        if (input.getId() != null) {
            qualificationsOptions = qualificationsOptionsDao.get(QualificationsOptions.class, input.getId());
            qualificationsOptions.setUpdatedAt(new Date());
        } else {
            qualificationsOptions = new QualificationsOptions();
            qualificationsOptions.setCreatedAt(new Date());
        }
        if(input.getName() != null){

            qualificationsOptions.setName(JSON.toJSONString(input.getName()));
        }
        if(input.getPid() != null){

            qualificationsOptions.setPid(input.getPid());
        }
        if(input.getType() != null){

            qualificationsOptions.setType(input.getType());
        }
        try{
            qualificationsOptionsDao.saveOrUpdate(qualificationsOptions);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

